package com.cskaoyan.service.impl;

import com.cskaoyan.bean.AdminInfoBean;
import com.cskaoyan.bean.InfoData;
import com.cskaoyan.bean.LoginUserData;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketRoleMapper;
import com.cskaoyan.mapper.RolePermissionsMapper;
import com.cskaoyan.mapper.RolePermissionsRelationMapper;
import com.cskaoyan.service.AuthService;
import com.cskaoyan.utils.Md5Util;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @description 后台登录的实现类
 * @date        2022.04.11 11:37
 * @package     com.cskaoyan.service.impl
 * @Classname   AuthServiceImpl
 * @author      rebaogu
 */
@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Autowired
    RolePermissionsRelationMapper rolePermissionsRelationMapper;

    @Autowired
    MarketRoleMapper marketRoleMapper;

    @Autowired
    RolePermissionsMapper rolePermissionsMapper;

    @SneakyThrows
    @Override
    public LoginUserData authLogin(String username, String password) {
        LoginUserData loginUserData = new LoginUserData();
        AdminInfoBean adminInfoBean = new AdminInfoBean();
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria adminCriteria = marketAdminExample.createCriteria();
        // String result1 = Md5Util.getMultyMd5(password);
        adminCriteria.andDeletedEqualTo(false).andUsernameEqualTo(username).andPasswordEqualTo(password);
        List<MarketAdmin> marketAdmins =
                marketAdminMapper.selectByExample(marketAdminExample);
        for (MarketAdmin marketAdmin : marketAdmins) {
            if (marketAdmin != null && !"".equals(marketAdmin)) {
                adminInfoBean.setNickName(marketAdmin.getUsername());
                adminInfoBean.setAvatar(marketAdmin.getAvatar());

                loginUserData.setAdminInfo(adminInfoBean);
            }
        }
        loginUserData.setAdminInfo(adminInfoBean);
        return loginUserData;
    }

    @Override
    public InfoData getInfo(MarketAdmin admin) {
        // 拿到管理员的角色id
        Integer[] roleIds = admin.getRoleIds();
        Set<String> permissions = new HashSet<>();
        Set<String> roles = new HashSet<>();
        InfoData infoData = new InfoData();

        if(roleIds.length==0){
            infoData.setName(admin.getUsername());
            infoData.setAvatar(admin.getAvatar());
            infoData.setPerms(permissions);
            infoData.setRoles(roles);
            return infoData;
        }


        // 根据角色id获取角色名
        // 根据角色id获得角色授权
        for (Integer roleId : roleIds) {
            if(roleId == 1){
                permissions.add("*");
                break;
            }
            List<String> results = rolePermissionsRelationMapper.selectPermissionsByRoleId(roleId);
            MarketRole marketRole = marketRoleMapper.selectByPrimaryKey(roleId);
            roles.add(marketRole.getName());

            RolePermissionsExample rolePermissionsExample = new RolePermissionsExample();
            RolePermissionsExample.Criteria criteria = rolePermissionsExample.createCriteria();
            criteria.andTypeEqualTo(3);
            criteria.andIdIn(results);
            List<RolePermissions> rolePermissions = rolePermissionsMapper.selectByExample(rolePermissionsExample);
            for (RolePermissions rolePermission : rolePermissions) {
                permissions.add(rolePermission.getApi());
            }
        }

        infoData.setName(admin.getUsername());
        infoData.setAvatar(admin.getAvatar());
        infoData.setPerms(permissions);
        infoData.setRoles(roles);



        return infoData;
    }
}
